package jmine.tec.hydra;

import java.io.IOException;
import java.io.InputStream;

import jmine.tec.hydra.domain.HydraActivation;
import jmine.tec.hydra.domain.HydraSystem;
import jmine.tec.hydra.domain.senderRegistry.FileSenderRegistry;
import jmine.tec.hydra.domain.specification.HydraSpecificationImpl;
import jmine.tec.script.persistent.domain.Script;
import jmine.tec.utils.io.IOUtils;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;
import bancosys.tec.services.testcase.RefDBTestCase;
import bancosys.tec.utils.db.descriptor.FlatReferenceDatabaseBuilder.ReferenceDatabaseDescriptionType;

/**
 * @author piercio
 */
public abstract class OldHydraTestCase extends RefDBTestCase {

    protected static final String SYSTEM = "MERCURIUS";

    protected static final String ACTIVATION = "LIQUIDACAO";

    protected static final String ACTION = "LIQ_COMPRA_DEFINITIVA_RF";

    protected static final String ACTIVATION_SEND_SCRIPT = "/scripts/MERCURIUS-SEND-ACTIVATION.bsh";

    protected static final String ACTIVATION_RECEIVE_SCRIPT = "/scripts/MERCURIUS-RECEIVE-ACTIVATION.bsh";

    protected static final String ENCODE_SCRIPT = "/scripts/MERCURIUS-SEL1052.bsh";

    protected static final String TEMPLATE = "/scripts/MERCURIUS-SEL1052.vm";

    protected static final String DECODE_SCRIPT = "/scripts/MERCURIUS-SEL1052R1.bsh";

    protected BaseDAO<HydraSystem> systemDAO;

    protected BaseDAO<HydraSpecificationImpl> specDAO;

    protected BaseDAO<Script> scriptDAO;

    protected BaseDAO<FileSenderRegistry> regDAO;

    protected BaseDAO<HydraActivation> activationDAO;

    public OldHydraTestCase() {
        super();
    }

    public OldHydraTestCase(String name) {
        super(name);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        this.systemDAO = super.getController().getDAOFactory().getGenericDAO(HydraSystem.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getControllerSpringId() {
        return "hydraController";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected HydraController getController() {
        return (HydraController) super.getController();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String[] getResourcesBaseReferencia() {
        return new String[]{ "hydra-sample-db.xml" };
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ReferenceDatabaseDescriptionType getReferenceDatabaseDescriptionType() {
        return ReferenceDatabaseDescriptionType.ALL;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean canOptimizeRestartBD() {
        return false;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getSpringMainXMLFilename() {
        return "jmine-tec-hydra-test.xml";
    }

    /**
     * Le um arquivo e devolve um string com o conteudo
     * 
     * @param file o nome do arquivo.
     * @return um string com o conteudo do stream.
     * @throws IOException caso não consiga ler o arquivo.
     */
    protected String read(String file) throws IOException {
        InputStream in = this.getClass().getResourceAsStream(file);
        byte[] text = IOUtils.readFully(in);
        return new String(text);
    }

    protected HydraSystem getMercurius() throws BeanNotFoundException {
        return this.systemDAO.findByNaturalKey(SYSTEM);
    }
}
